package models

import (
	"MoSkeleton/framework/core/motypes"
	"MoSkeleton/models/base"
	"gorm.io/gorm"
)

type MoModel struct {
	ID        uint64          `gorm:"primarykey;autoIncrement=false" json:"id,string"`
	CreatedAt *motypes.MoTime `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt *motypes.MoTime `gorm:"autoUpdateTime" json:"updatedAt"`
}

type IDModel struct {
	ID uint64 `json:"id,string" form:"id,string"`
}

type MoUserModel struct {
	CreatedBy string `gorm:"column:created_by;type:VARCHAR(32);" json:"createdBy"`
	UpdatedBy string `gorm:"column:updated_by;type:VARCHAR(32);" json:"updatedBy"`
}

func (m *MoModel) DeleteByIds(tx *gorm.DB, ids *[]uint64, t any) (bool, error) {
	result := base.GetModelDB(tx).Where("id in (?)", *ids).Delete(t)
	return result.RowsAffected > 0, result.Error
}

func (m *MoModel) GetById(tx *gorm.DB, id uint64, t any) error {
	return base.GetModelDB(tx).Where("id = ?", id).First(t).Error
}

func (b *MoModel) ChangeStatus(tx *gorm.DB, t any) error {
	return base.GetModelDB(tx).Updates(t).Error
}

func (b *MoModel) UpdateById(tx *gorm.DB, t any) error {
	return base.GetModelDB(tx).Updates(t).Error
}

func (b *MoModel) Save(tx *gorm.DB, t any) error {
	return base.GetModelDB(tx).Save(t).Error
}
